package Q2;

public class Test {
    public static void main(String[] args){
        Node n1 = new Node(0);
        Node n2 = new Node(0);
        Node p1 = n1;
        Node p2 = n2;
        for(int i=0;i<10;i++){
            Node a1 = new Node(i);
            Node a2 = new Node(i);
            p1.setNext(a1);
            p2.setNext(a2);
            p1 = a1;
            p2 = a2;
        }
        p2.setNext(n2);
        System.out.println(hasRing(n1));
        System.out.println(hasRing(n2));
    }

    public static boolean hasRing(Node n) {
        Node fast = n;
        Node slow = n;
        while(slow!=null&&fast!=null&&fast.getNext()!=null){
            fast = fast.getNext().getNext();
            slow = slow.getNext();
            if(fast==slow){
                return true;
            }
        }
        return false;
    }


}

class Node{
    private int value;
    private Node next;

    public Node(int value){
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
